package org.mcflier.appuntamento.tasks;

import org.mcflier.appuntamento.core.Factory;
import org.mcflier.appuntamento.core.exception.ModulesException;
import org.mcflier.appuntamento.core.modules.ModulesManager;
import org.mcflier.appuntamento.tasks.exception.CreatingTaskException;

public class ReloadModulesTask extends AbstractTask {
        
        private Factory factory;
        private ModulesManager modules;

        public ReloadModulesTask(String name, long delay) throws CreatingTaskException, ModulesException {
                super(name, delay);
                
                this.factory = Factory.getInstance();
                
                try {
                        this.modules = (ModulesManager)this.factory.getModuleInstance(Factory.MODULE_MODULES);
                } catch (ModulesException e) {
                        throw new CreatingTaskException(this.getClass().getSimpleName()+": Error loading the necessary modules", e);
                }
        }

        @Override
        public void doSomething() {
                this.logger.info("Task "+this.getClass().getSimpleName()+": Reloading modules");
                try {
                        this.modules.loadModules();
                } catch (ModulesException e) {
                        this.logger.warning("Error executing task "+this.getClass().getSimpleName()+": Reloading modules failed.\n"+e.getMessage());
                }
        }
}